package com.icode.core.shared;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: ZhongGang
 * Date: 13-6-22
 * Time: 下午4:10
 */
public class JodaTimeUtils {

    public static DateTime now() {
        return new DateTime();
    }

    public static LocalDate StringToLocalDate(String date) {
        String year = date.substring(0, 4);
        String month = date.substring(5, 7);
        String day = date.substring(8);
        return new LocalDate(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day));
    }

    public static String getTime(String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = new Date();
        return sdf.format(date);
    }

    public static boolean isTimeOut(String orderTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String current = sdf.format(new Date());
        long hour = 0;
        long min = 0;
        long s = 0;
        try {
            Date now = sdf.parse(current);
            Date date = sdf.parse(orderTime);
            long l = now.getTime() - date.getTime();
            long day = l / (24 * 60 * 60 * 1000);
            hour = (l / (60 * 60 * 1000) - day * 24);
            min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
            s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (hour >= 12) {
            return true;
        }
        return false;
    }

    public static String getSerial() {
        Random r = new Random();
        int number = r.nextInt(9000) + 1000;
        return String.valueOf(number);
    }

    public static int getValueOfTwoDate(String startDate, String endDate) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        long to = 0;
        try {
            to = df.parse(endDate).getTime();
            long from = df.parse(startDate).getTime();
            return (int) ((to - from) / (1000 * 60 * 60 * 24));
        } catch (ParseException e) {
            return 7;
        }
    }
}
